package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.entity.Address;
import com.qf.entity.Order;
import com.qf.utils.DruidUtils;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/14 22:27
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void addOrder(Order order) {
        QueryRunner qr=new QueryRunner();
        Connection conn = DruidUtils.getConnection();
        Object[] params={order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            qr.update(conn,"insert  into tb_order values (?,?,?,?,?,?)",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public List<Order> getOrderByUid(Integer id) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        try {
            return qr.query("select a.*,o.id as oid,o.money,o.status,o.time from tb_order as o left OUTER join tb_address as a on o.aid=a.id where o.uid=?", new ResultSetHandler<List<Order>>() {
                @Override
                public List<Order> handle(ResultSet resultSet) throws SQLException {
                    List<Order> orderList=new LinkedList<>();
                    while (resultSet.next()){
                        int aid = resultSet.getInt("id");
                        String detail = resultSet.getString("detail");
                        String name = resultSet.getString("name");
                        String phone = resultSet.getString("phone");
                        int level = resultSet.getInt("level");
                        String oid = resultSet.getString("oid");
                        BigDecimal money = resultSet.getBigDecimal("money");
                        String status = resultSet.getString("status");
                        Date time = resultSet.getTimestamp("time");
               Order order=new Order(oid,id,status,money,time,aid,new Address(aid,detail,name,phone,id,level));
                        orderList.add(order);
                    }
                    return orderList;
                }
            }, id);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public Order queryOrderByOid(String oid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        try {
            return qr.query("select * from tb_order where id=?",new BeanHandler<>(Order.class),oid);
        } catch (SQLException throwables) {
           throw new RuntimeException(throwables);
        }

    }

    @Override
    public void updateOrderByOrder(Order order) {

        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={order.getStatus(),order.getId()};
        try {
            qr.update("update tb_order set status=? where id=?",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }


    }
}
